from openapi_spec_validator import validate_spec
from jsonref import JsonRef
import json
import yaml

def resolve_and_mark_recursive(obj, path="", seen=None):
    if seen is None:
        seen = set()
    if isinstance(obj, dict):
        for key, value in obj.items():
            if key == "$ref":
                if value in seen:
                    return {"recursive_ref": value}
                seen.add(value)
            obj[key] = resolve_and_mark_recursive(value, path + "/" + key, seen)
    elif isinstance(obj, list):
        for i, item in enumerate(obj):
            obj[i] = resolve_and_mark_recursive(item, path + "/" + str(i), seen)
    return obj


with open("test.yaml", 'r', encoding='utf') as f:
    your_yaml_string = f.read()
# 假设你的 OpenAPI YAML 已经加载为字符串
spec_dict = JsonRef.replace_refs(yaml.safe_load(your_yaml_string))
#validate_spec(spec_dict)  # 可选，验证规范
marked_spec = resolve_and_mark_recursive(spec_dict)

# 打印结果
print(json.dumps(marked_spec, indent=2))